package net.web{  
	
	import flash.net.Responder;
	
	/** 
	 * Operation  
	 */  
	public class Operation{  
		
		private var _service:Service;  
		private var _name:String;  
		private var _responder:Responder;
		
		private var _callBackResultHandler:Function;
		private var _callBackFaultHandler:Function;
		
		/**
		 * 构成通道
		 * @param service
		 * @param name
		 * @param callBackResultHandler
		 * @param callBackFaultHandler
		 * 
		 */		
		public function Operation(service:Service,name:String,callBackResultHandler:Function,callBackFaultHandler:Function) {  
			
			_callBackResultHandler = callBackResultHandler;
			_callBackFaultHandler = callBackFaultHandler;
			
			_service = service;  
			_name = name;  
			_responder = new Responder ( resultHandler, statusHandler );              
		}
		
		/** 
		 * 服务名
		 */  
		public function get service():Service{  
			return _service;  
		}  
		
		/**
		 * 方法名
		 * @return 
		 */		
		public function get name():String{  
			return _name;  
		}  
		
		/**
		 * 请求数据
		 * @param args
		 */		
		public function send(...args:Array):void{                                     
			var params:Array = [_service.destination + "." + _name,_responder];  
			params = params.concat(args);  
			_service.connenction.call.apply(_service.connenction,params);  
		}  
		
		
		/** 
		 * 返回结果的处理 
		 */  
		private function resultHandler(info:Object):void{
			if(_callBackResultHandler!=null)
				_callBackResultHandler(info);
			
			dispose();
		}  
		
		/**
		 * 异常的处理
		 * @param info
		 */		
		private function statusHandler(info:Object):void
		{
			if(_callBackFaultHandler!=null)
				_callBackFaultHandler(info);
			
			dispose();
		}
		
		/**
		 * 销毁
		 */		
		private function dispose():void
		{
			_name = null;
			_service = null;
			_responder = null;
			
			_callBackResultHandler = null;
			_callBackFaultHandler = null;
		}
	}  
}  